const CompressionWebpackPlugin = require("compression-webpack-plugin")

module.exports = {
  configureWebpack: {
    plugins: [
      new CompressionWebpackPlugin({
        filename: "[path][base].gz",
        algorithm: "gzip",
        test: /\.js$/,
        threshold: 10240,
        minRatio: 0.8,
        exclude: /node_modules/
      })
    ],
    optimization: {
      splitChunks: {
        cacheGroups: {
          commonComponents: {
            name: "chunk-common-component",
            test: /src\/components/,
            chunks: "all",
            minChunks: 10, // 只有引用超过10次的组件才打进此包
            maxSize: 500000, // 因为公共组件也很大, 所以限制打包大小为500K
            priority: 11 // 优先级11, 先进行此分包操作
          },
          components: {
            name: "chunk-component",
            test: /src\/components/,
            chunks: "all",
            minChunks: 4, // 只有引用超过4次才打进此包
            maxSize: 500000, // 由于公共组件很大, 允许最大打包大小为500K
            reuseExistingChunk: true, // 重复利用已存在的包
            priority: 10
          },
          common: {
            chunks: "initial",
            minChunks: 2,
            priority: -20
          }
        }
      }
    },
    devServer: {
      port: "8080",
      disableHostCheck: true,
      proxy: {
        "/dev": {
          target: "https://tkzc-release-napi.zmaxfilm.com",
          changeOrigin: true,
          pathRewrite: {
            "^/dev": ""
          }
        },
        "/prod": {
          target: "https://ilike-cloud-mapi.zmaxfilm.com",
          changeOrigin: true,
          pathRewrite: {
            "^/prod": ""
          }
        },
        "/zuimei-film-oss": {
          target: "https://public-oss-file.zmaxfilm.com",
          changeOrigin: true,
          pathRewrite: {
            "^/zuimei-film-oss/": ""
          }
        }
      }
    }
  },
  chainWebpack: (config) => {
    config.plugin("define").tap((definitions) => {
      definitions[0]["process.env"].BUILD_TIME = JSON.stringify(new Date().toLocaleString("zh", { hour12: false }).replaceAll("/", "-"))
      return definitions
    })
  }
}
